package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.mapper.FoodMapper;
import com.itheima.pojo.Food;
import com.itheima.pojo.Foodattribute;
import com.itheima.pojo.Paging;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@Service
public class FoodService {
    @Resource
    private FoodMapper foodMapper;

    /**
     * H13-菜品新增
     */
    public Integer add(Food food) {
        //获取口味集合
        List<Foodattribute> flavors = food.getFlavors();
        //获取菜品中的分类id
        String categoryId = food.getCategoryId();
        int i = Integer.parseInt(categoryId);
        //通过分类id获取分类名字并写入
        String j = foodMapper.j(i);
        food.setCategoryName(j);
        //新增菜品
        foodMapper.add(food);
        //获取菜品id
        Integer id = food.getId();
        String s = String.valueOf(id);
        Integer count = 0;
        if (flavors == null) {
            return count;
        }
        //遍历口味并写入
        for (Foodattribute flavor : flavors) {
            if (flavor == null) {
                continue;
            }
            flavor.setDishId(s);
            foodMapper.adds(flavor);
            count++;
        }
        return count;
    }

    /**
     * H12-菜品分页查询
     *
     * @param name     名字
     * @param page     页数
     * @param pageSize 每页条数
     */
    public R select(Integer page, Integer pageSize, String name) {
        //构建分页拦截器
        //PageHelper.startPage(m,n)两个参数，第一个参数是页数。第二个参数是条数，每页查询的条数。
        PageHelper.startPage(page, pageSize);

        List<Food> employeeList = foodMapper.all(name);

        PageInfo<Food> pageInfo = new PageInfo<>(employeeList);

        //返回值对象，这里的P类是根据前端要的数据专门写的类。属性只有这四条
        Paging p = new Paging();
        //总条数
        p.setTotal(pageInfo.getTotal());
        //每页显示条数
        p.setSize(pageInfo.getSize());
        //当前页码
        p.setCurrent(pageInfo.getPageNum());
        //被展示的数据集
        p.setRecords(employeeList);
        return R.success(p);

    }

    /**
     * H12-菜品下拉列表
     *
     * @param categoryId 分类id
     * @return 菜品集合
     */
    public List<Food> list(String categoryId) {
        return foodMapper.list(categoryId);
    }

    /**
     * H12-根据ID查询菜品
     */
    public Food foodAll(Integer id) {
        Food food = foodMapper.a(id);
        String s = String.valueOf(id);

        List<Foodattribute> foodattributes = foodMapper.foodSll(s);
        food.setFlavors(foodattributes);
        return food;
    }

    /**
     * H14-菜品修改
     */
    public Integer update(Food food) {
        //获取口味集合
        List<Foodattribute> flavors = food.getFlavors();
        //获取食物类别id
        String categoryId = food.getCategoryId();
        int i = Integer.parseInt(categoryId);
        //通过id获取分类中菜品和套餐名
        String j = foodMapper.j(i);
        //再把菜名设置到食物中
        food.setCategoryName(j);
        Integer id = food.getId();
        String s = String.valueOf(id);
        //删除所有口味
        foodMapper.deleter(s);
        int count = 0;
        if (flavors == null) {
            return count;
        }
        for (Foodattribute flavor : flavors) {
            if (flavor == null) {
                continue;
            }
            //设置食物中的id到口味中
            flavor.setDishId(s);
            foodMapper.update(food);
            foodMapper.adsss(flavor);
            count++;
        }
        return count;
    }

    /**
     * H15-批量删除菜品
     */
    public Integer delete(String ids) {
        String[] split = ids.split(",");
        int coumt = 0;
        if (split.length == 0) {
            return coumt;
        }
        for (String s : split) {
            if (s == null) {
                continue;
            }
            int i = Integer.parseInt(s);
            foodMapper.delete(i);
            foodMapper.deletes(s);
            coumt++;
        }
        return coumt;
    }

    /**
     * F16-菜品停售/起售
     */
    public Integer statusUpdate(String ids) {
        String[] split = ids.split(",");

        int count = 0;
        for (String s : split) {

            int i = Integer.parseInt(s);

            Integer integer1 = foodMapper.sUpdate(i);

            Integer integer;
            if (integer1 != 0) {
                integer = foodMapper.statusUpdate(i);
            } else {
                integer = foodMapper.staUpdate(i);
            }
            if (integer != 0) {
                count++;
            }
        }
        return count;
    }
}
